package hejie.admin.service.impl;

import java.util.List;

import hejie.admin.dao.IndustryTypeDao;
import hejie.admin.service.IndustryTypeService;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import publics.model.IndustryType;
import publics.util.Page;


@Component("industryTypeService")
public class IndustryTypeServiceImpl implements IndustryTypeService{
	
	IndustryTypeDao industryTypeDao;
	
	public IndustryTypeDao getIndustryTypeDao() {
		return industryTypeDao;
	}	
	@Resource(name="industryTypeDao")
	public void setIndustryTypeDao(IndustryTypeDao industryTypeDao) {
		this.industryTypeDao = industryTypeDao;
		System.out.println("industryTypeDaoImpl inject in IndustryTypeServiceImpl");
	}

	public Object saveIndustryType(IndustryType itype) {
		List<IndustryType> itypeName = industryTypeDao.findByName(itype.getTypeName());	
		List<IndustryType> itypeNum = industryTypeDao.findByNum(itype.getTypeNum());	
		if(itypeName.size() == 0&&itypeNum.size()==0){
			return industryTypeDao.save(itype);		
		}
		else 			
		   return null;
	}

	public Page findByPage(Page page) {
		String sql = "from IndustryType i";		 
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接
			sql = sql + " where ";  
			for(int i = 0; i < page.getConditions().size(); ){
				sql = sql + "( i.ltypeName like '%"+page.getConditions().get(i)+"%'"
				+" or i.ltypeNum like '%"+page.getConditions().get(i)+"%'"
				+" or i.typeName like '%"+page.getConditions().get(i)+"%'"
				+" or i.typeNum like '%"+page.getConditions().get(i)+"%' ) ";
				//每次上面两个属性只有匹配到的才能取到值，不然为%
				i++;
				if(i < page.getConditions().size()){// 如果条件不止一个 进行连续拼接
					sql = sql + " and ";
				}
			}
		}
		System.out.println("sql ="+sql);
		
		int pageSize = page.getLimit();
		int offset = page.getStart();//-1)*pageSize;
		page.setRoot(industryTypeDao.findByPage(sql,offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(industryTypeDao.findByCount(sql));   //把数据总数放入totalProperty中
		return page;
	}

	public Page findLtypeByPage(Page page){
		//String sql = "select distinct Ind_ltype_name,Ind_ltype_num from z_industry_type";	//这句话在数据库里直接查询运行可以找出不重复的大类名和大类代码	 
		String sql = "select distinct new IndustryType(i.ltypeName, i.ltypeNum) from IndustryType i";
		//String sql = "from IndustryType i where i.ltypeName in (select i.ltypeName from IndustryType i group by i.ltypeName having count(i.ltypeName)>1)";
		System.out.println("sql ="+sql);		
		int pageSize = page.getLimit();
		int offset = page.getStart();//-1)*pageSize;
		page.setRoot(industryTypeDao.findByPage(sql,offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(industryTypeDao.findByCount(sql));   //把数据总数放入totalProperty中
		return page;
	}
	
	public boolean updateIndustryType(IndustryType itype) {
		return industryTypeDao.update(itype);

	}

	public boolean deleteIndustryType(Integer id) {
		System.out.println("service里 industryTypeId="+id);
		return industryTypeDao.deleteById(id);
	} 	
	
}
